package com.sykj.springboot.thread;

import com.sykj.springboot.common.utils.TimeUtils;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class SellerThred implements Runnable{

    private String name;

    public SellerThred(String name,Integer count, Integer totalCount) {
        this.count = count;
        this.totalCount = totalCount;
        this.name = name;
    }

    @Override
    public void run() {
        while (count>0){
            extracted();
            System.out.println(TimeUtils.getCurrentDateTime()+":" +name+"卖一张:剩余："+count+":卖出："+totalCount);
        }
    }

    private  void extracted() {
        count --;
        totalCount ++;
    }

    static Integer totalCount = 0;
    static Integer count = 10;

    /**
     * @author lining
     * @date 2022/6/30 15:28
     * @description 保证线程安全的方式一   加synchronized代码块
     **/
    public static void main(String[] args) {

        ExecutorService executor = Executors.newFixedThreadPool(2);
        for (int i = 0; i < 2; i++) {
            executor.submit(new SellerThred("线程"+i,count,totalCount));
        }
    }

}
